package com.zhangbo.share.dubbo.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @author zhangbo
 * @version 1.00
 * @classdesc <pre></pre>
 * @create 2021/12/19 14:31
 * @update <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           zhangbo     2021/12/19 14:31
 *     -----------------------------------------------------------
 * </pre>
 */
@Slf4j
@Activate(group = "consumer")
public class ConsumerFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        log.info("dubbo调用请求参数：{}", invocation.getArguments());

        Result invoke = null;
        invoke = invoker.invoke(invocation);
        if (null == invoke.getValue() && null != invoke.getException()) {
            String exceptionMessage = invoke.getAttachment("exception");
            if (exceptionMessage != null) {
                log.info("dubbo调用请求参数：{}，业务异常：{}", invocation.getArguments(), exceptionMessage);
                throw new RpcException(exceptionMessage);
            }
        }

        log.info("dubbo调用请求参数：{}，响应参数：{}", invocation.getArguments(), invoke.getValue());
        return invoke;
    }
}
